package edu.pku.sei.modeler.uml.obsolete.fromdiagram;

import org.eclipse.gef.commands.Command;

import edu.pku.sei.modeler.exceptions.FactoryNotFoundException;
import edu.pku.sei.modeler.model.common.ModelRegistry;
import edu.pku.sei.modeler.model.uml.Interfaces.Interface;
import edu.pku.sei.modeler.model.uml.Kernel.Classifier;
import edu.pku.sei.modeler.model.uml.Kernel.Property;
import edu.pku.sei.modeler.model.uml.Kernel.UMLClass;
import edu.pku.sei.modeler.uml.modelHelper.ModelFactory;

public class CreatePropertyCommand extends Command {
	
	private Classifier classifier;
	
	private Property property;

	public CreatePropertyCommand(Classifier classifier) {
		this.classifier  = classifier;
		
		try {
			this.property = ((ModelFactory)(ModelRegistry.getRegistry().getFactory(classifier.getModel()))).createProperty();
							
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FactoryNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Override
	public boolean canExecute() {
		//return true;
		return classifier != null;
	}

	@Override
	public void execute() {
		if (classifier instanceof Interface) {
			((Interface)classifier).getOwnedAttribute().add(property);
		}else if (classifier instanceof UMLClass) {
			((UMLClass)classifier).getOwnedAttribute().add(property);
		}
		
	}

	@Override
	public void undo() {
		if (classifier instanceof Interface) {
			((Interface)classifier).getOwnedAttribute().remove(property);
		}else if (classifier instanceof UMLClass) {
			((UMLClass)classifier).getOwnedAttribute().remove(property);
		}
		
	}

	@Override
	public boolean canUndo() {
		return classifier != null;
	}
	
	
	
	
	

}
